package main

import (
	"bytes"
	"fmt"

	oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

var BucketName = "xx"
var EndPoint = "oss-cn-beijing.aliyuncs.com"
var AccessKeyId = "xx"
var AccessKeySecret = "xx"

func putObject(data *bytes.Buffer) {
	client, err := oss.New(EndPoint, AccessKeyId, AccessKeySecret)
	if err != nil {
		fmt.Printf("create error %+v", err)
		return
	}

	bucket, err := client.Bucket(BucketName)
	if err != nil {
		fmt.Printf("get bucket error %+v", err)
		return
	}

	err = bucket.PutObject("img.jpeg", data)
	if err != nil {
		fmt.Printf("put error %+v", err)
		return
	}
}

func putObjectFromFile() {
	client, err := oss.New(EndPoint, AccessKeyId, AccessKeySecret)
	if err != nil {
		fmt.Printf("create error %+v", err)
		return
	}

	bucket, err := client.Bucket(BucketName)
	if err != nil {
		fmt.Printf("get bucket error %+v", err)
		return
	}

	err = bucket.PutObjectFromFile("dev/README.md", "README.md")
	if err != nil {
		fmt.Printf("put error %+v", err)
		return
	}
}

func getObject() {
	client, err := oss.New(EndPoint, AccessKeyId, AccessKeySecret)
	if err != nil {
		fmt.Printf("create error %+v", err)
		return
	}

	bucket, err := client.Bucket(BucketName)
	if err != nil {
		fmt.Printf("get bucket error %+v", err)
		return
	}

	err = bucket.GetObjectToFile("dev/README.md", "README.local.md")
	if err != nil {
		fmt.Printf("get error %+v", err)
		return
	}
}

func deleteObject() {
	client, err := oss.New(EndPoint, AccessKeyId, AccessKeySecret)
	if err != nil {
		fmt.Printf("create error %+v", err)
		return
	}

	bucket, err := client.Bucket(BucketName)
	if err != nil {
		fmt.Printf("get bucket error %+v", err)
		return
	}

	err = bucket.DeleteObject("dev")
	if err != nil {
		fmt.Printf("delete error %+v", err)
		return
	}
}

func main() {
	putObjectFromFile()
	getObject()
	deleteObject()
}
